<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2019/4/11
 * Time: 20:36
 */

namespace app\Module\Index\Controller;


use app\Module\Index\Logic\MessageLogic;
use app\Module\Index\Model\UnreadChatModel;
use app\Module\Index\Model\UnreadCommentModel;
use app\Module\Index\Model\UnreadReplyModel;
use framework\controller\Controller;

class MessageController extends Controller
{
    public function readAction($id)
    {
        $messageLogic = new MessageLogic();
        $messageLogic->read($id);
        return success(true);
    }

    /**
     * 获取消息列表
     *
     * @return string
     */
    public function getListAction($page = 1, $limit = 10)
    {
        $userId = session('userId');
        if (empty($userId)) {
            return success([]);
        }

        $messageLogic = new MessageLogic();
        $data         = $messageLogic->lists($userId, $page, $limit);

        return success($data);

    }

    public function getUnreadNumAction()
    {
        $userId = session('userId');
        if (empty($userId)) {
            return success(0);
        }

        $unreadReplyModel   = new UnreadReplyModel();
        $unreadCommentModel = new UnreadCommentModel();
        $unreadChatModel    = new UnreadChatModel();

        $replyCount   = $unreadReplyModel->sum('num', ['target_user_id' => $userId], 600);
        $commentCount = $unreadCommentModel->sum('num', ['target_user_id' => $userId], 600);
        $chatCount    = $unreadChatModel->sum('num', ['target_user_id' => $userId], 600);

        $count = $replyCount + $commentCount + $chatCount;
        return success($count);
    }


    public function getUnreadNumV2Action()
    {
        $userId = session('userId');
        if (empty($userId)) {
            return success(0);
        }

        $unreadChatModel = new UnreadChatModel();
        $chatCount       = $unreadChatModel->sum('num', ['target_user_id' => $userId], 600);

        $messageLogic = new MessageLogic();
        $messageCount = $messageLogic->count($userId);

        $data = [
            'message' => $messageCount,
            'chat'    => empty($chatCount) ? 0 : $chatCount,
        ];

        return success($data);
    }
}